package com.mxx.basic.server.retrofit.manager;


import com.mxx.basic.utils.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class RetrofitManager {
    static volatile RetrofitManager retrofitManager;
    static Map<String, String> urlMaps;
    static Map<String, Object> retrofitMaps;
    static OkHttpClient okHttpClient;
    static String protocol_host_name_port;

    public static RetrofitManager getInstance() {
        if (retrofitManager == null) {
            synchronized (RetrofitManager.class) {
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    /**
     * 获取Retrofit的实例
     *
     * @param baseUrl protocol_host_name_port
     * @return Retrofit
     */
    public Retrofit getRetrofit(String baseUrl) {
        if (baseUrl != null && !"".equals(baseUrl)) {
            protocol_host_name_port = baseUrl;
        }
        return createRetrofit();
    }

    /**
     * 获取Retrofit的实例
     *
     * @param baseUrl protocol_host_name_port
     * @param client  OkHttpClient的实例
     * @return Retrofit
     */
    public Retrofit getRetrofit(String baseUrl, OkHttpClient client) {
        if (baseUrl != null && !"".equals(baseUrl)) {
            protocol_host_name_port = baseUrl;
        }
        if (client != null) {
            okHttpClient = client;
        }
        return createRetrofit();
    }

    /**
     * 实例化 Retrofit
     *
     * @return Retrofit
     */
    private Retrofit createRetrofit() {
        boolean retrofitExist = retrofitExist(protocol_host_name_port, protocol_host_name_port);
        Retrofit retrofit = null;
        Logger mLogger= Logger.getInstance();
        if (!retrofitExist) {
            //如果Retrofit的实例不存在，创建Retrofit实例
            urlMaps.put(protocol_host_name_port, protocol_host_name_port);
            Retrofit.Builder mBuilder = new Retrofit.Builder();
            try {
                mBuilder.baseUrl(protocol_host_name_port);
            } catch (Exception e) {
                e.printStackTrace();
                mLogger.e(e.getMessage());
            }
            mBuilder.client(okHttpClient);
            mBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());

            retrofit = mBuilder.build();
            if (retrofitMaps == null) {
                retrofitMaps = new HashMap<>();
            }
            retrofitMaps.put(protocol_host_name_port, retrofit);
        } else {
            //如果Retrofit的实例存在，从Retrofit的Map集合中根据key取出对应的Retrofit实例
            if (retrofitMaps == null) {
                retrofitMaps = new HashMap<>();
                return null;
            }
            if (retrofitMaps.isEmpty()) {
                return null;
            }
            Set<String> keys = retrofitMaps.keySet();
            for (String key : keys) {
                if (key.equals(protocol_host_name_port)) {
                    retrofit = (Retrofit) retrofitMaps.get(key);
                }
            }
        }
        return retrofit;
    }

    /**
     * @param urlKey   存储baseUrl集合的key(以baseUrl为Key)
     * @param urlValue 存储baseUrl集合的value(以baseUrl为value)
     * @return boolean 默认为true
     */
    private boolean retrofitExist(String urlKey, String urlValue) {
        if (urlMaps == null) {
            urlMaps = new HashMap<>();
            return false;
        }
        if (urlMaps.isEmpty()) {
            urlMaps.put(urlKey, urlValue);
            return false;
        }
        if (!urlMaps.containsValue(urlValue)) {
            urlMaps.put(urlKey, urlValue);
            return false;
        }
        return true;
    }

}
